/*
 * Copyright (C) 2017  ShenZhen xinLianZhengXin Co.,Ltd All Rights Reserved.
 * δ������˾��ʽ����ͬ�⣬�����κθ��ˡ����岻��ʹ�á����ơ��޸Ļ򷢲������.
 * ��Ȩ���������������������޹�˾ http://www.credlink.com/
 */
package com.blolink.manager.service.bas.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.blolink.bean.bo.UpdateStatusBo;
import com.blolink.bean.po.bas.BasUserInfoPo;
import com.blolink.dao.bas.BasUserInfoDao;
import com.blolink.manager.bean.DataGridReqBean;
import com.blolink.manager.bean.DataGridResBean;
import com.blolink.manager.bean.UpdateStatusBatchBean;
import com.blolink.manager.constants.Const;
import com.blolink.manager.constants.RetCode;
import com.blolink.manager.service.bas.BasUserInfoService;
import com.blolink.manager.util.BeanUtil;
import com.blolink.manager.util.ServiceException;
import com.blolink.manager.util.bean.ServiceCommonUtils;

/**
 * UserServiceImpl
 * 
 * @version 2017年12月6日上午10:17:06
 * @author wuliu
 */
@Service
@Transactional
public class BasUserInfoServiceImpl implements BasUserInfoService{

    private static final Logger log = LoggerFactory.getLogger(BasUserInfoServiceImpl.class);
    
    @Autowired
    private BasUserInfoDao basUserInfoDao;

    @Override
    public void insert(BasUserInfoPo entity) {
        basUserInfoDao.insert(entity);
    }
    
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public BasUserInfoPo find(BasUserInfoPo info) {
        return basUserInfoDao.find(info);
    }
    
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public List<BasUserInfoPo> findAll(BasUserInfoPo info) {
        return basUserInfoDao.findAll(info);
    }

    @Override
    public void update(BasUserInfoPo info, Boolean limit) {
        int count = basUserInfoDao.update(info);
        if(limit){
            if(count != 1){
                throw new ServiceException(RetCode.UPDATE_FAIL_CODE_N, RetCode.UPDATE_FAIL_CODE_N_MSG);
            }
        }
    }

    @Override
    public void update(BasUserInfoPo info, Integer num) {
        int count = basUserInfoDao.update(info);
        if(num.intValue() != count){
            throw new ServiceException(RetCode.UPDATE_FAIL_CODE_N, RetCode.UPDATE_FAIL_CODE_N_MSG);
        }    
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public void loadDataGrid(DataGridResBean res, DataGridReqBean req) {
        //默认返回值
        res.setTotal(0L);
        res.setRows(new ArrayList<Map<String,Object>>());
        //查询条件
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map = BeanUtil.objectToMapByIntrospector(req.getBean());
        }
        catch (Throwable e) {
            log.error("xxxxxx用户管理查询系统异常",e);
            return;
        }
        map.put("pageStart", req.getPageStart());
        map.put("pageSize", req.getRows());
        //查询数量
        Long total = basUserInfoDao.userManagerQueryCount(map);
        res.setTotal(total);
        //查询数据
        List<Map<String,Object>> list = basUserInfoDao.userManagerQuery(map);
        if(list == null || list.isEmpty()){
            list = new ArrayList<Map<String,Object>>();
        }
        res.setRows(list);
    }

    @Override
    public void updateStatusBatch(UpdateStatusBatchBean updateStatusBatchBean) {
       int idsLen = updateStatusBatchBean.getIds().split(Const.IDS_SEPARATOR).length;
       UpdateStatusBo bo = ServiceCommonUtils.getUpdateStatusBo(updateStatusBatchBean);
       int count = basUserInfoDao.updateStatusBatch(bo);
       if(idsLen != count){
           log.info("需要更新数量:{},被更新数量:{}",updateStatusBatchBean.getIds(),count);
           throw new ServiceException(RetCode.UPDATE_FAIL_CODE_N, RetCode.UPDATE_FAIL_CODE_N_MSG);
       }
    }

}
